package org.roma.ninemen;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;



/**
 * 
 * @author Mathias Larsson and Robin Weidelid
 *
 */
public class NineMenRoMaActivity extends Activity {
    /** Called when the activity is first created. */
	public static final int FROMGAMEBOARD = 1;
	public static final int FROMACTIVITY = 2;
	public static final String SHAREDPREF = "ninemenroma";
	private GameBoard iv;
	private LinearLayout ll;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        DisplayMetrics metrics = new DisplayMetrics();
    	getWindowManager().getDefaultDisplay().getMetrics(metrics);
        iv = new GameBoard(NineMenRoMaActivity.this, metrics.widthPixels, metrics.heightPixels);
        ll = new LinearLayout(NineMenRoMaActivity.this);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0, 1, Menu.NONE, "New game");
    	menu.add(0, 2, Menu.NONE, "Save game");
    	menu.add(0, 3, Menu.NONE, "Load game");
    	menu.add(0, 4, Menu.NONE, "Settings");
        return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		
		case 1:
			iv.reset();
			return true;
		
		case 2:
			Dialog savedlg = new SaveDialog(this);
			savedlg.show();
			return true;
		
		case 3:
			Dialog loaddlg = new LoadDialog(this, FROMGAMEBOARD);
			loaddlg.show();
			return true;
			
		case 4:
			Intent i = new Intent(NineMenRoMaActivity.this, SettingsActivity.class);
			startActivity(i);
		}
		return false;
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		iv.start();
	}

	@Override
	protected void onStop() {
		super.onStop();
		iv.save(10);
		iv.stop();
	}
	
	public void continueGame(View view){
		iv.load(10);
        ll.addView(iv);
        setContentView(ll);
	}
	
	public void newGame(View view){
		iv.reset();
        ll.addView(iv);
        setContentView(ll);
	}
	
	public void loadGame(View view){
		Dialog loaddlg = new LoadDialog(this, FROMACTIVITY);
		loaddlg.show();
	}
	
	public void gotoSettings(View view){
		Intent i = new Intent(NineMenRoMaActivity.this, SettingsActivity.class);
		startActivity(i);
	}
	
	private class SaveDialog extends Dialog{

		private ListView list;
		
		public SaveDialog(Context context) {
			super(context);
		}
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			DatabaseHelper db = new DatabaseHelper(NineMenRoMaActivity.this);
			String[] values = db.getGames();
			super.onCreate(savedInstanceState);
			setContentView(R.layout.savelayout);
			
			setTitle("Save Game");
			list = (ListView) findViewById(R.id.saveList);
			list.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					// TODO Auto-generated method stub
					iv.save(position);
					dismiss();
				}
			});
			
			GamesArrayAdapter adapter = new GamesArrayAdapter(NineMenRoMaActivity.this, values);
			list.setAdapter(adapter);
			
		}

		
	}

	private class LoadDialog extends Dialog{
		private ListView list;
		private int from;
		
		public LoadDialog(Context context, int from) {
			super(context);
			this.from = from;
		}
		
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			DatabaseHelper db = new DatabaseHelper(NineMenRoMaActivity.this);
			String[] values = db.getGames();
			super.onCreate(savedInstanceState);
			setContentView(R.layout.savelayout);
			
			setTitle("Load Game");
			list = (ListView) findViewById(R.id.saveList);
			list.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
					// TODO Auto-generated method stub
					iv.load(position);
					if(from == FROMACTIVITY){
					     ll.addView(iv);
					     NineMenRoMaActivity.this.setContentView(ll);
					}
					dismiss();
				}
			});
			
			GamesArrayAdapter adapter = new GamesArrayAdapter(NineMenRoMaActivity.this, values);
			list.setAdapter(adapter);
			
		}
	}

    
}